<?php

namespace App\Models\ZZMed;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;

class ZHospMember extends Authenticatable implements JWTSubject
{
    use Notifiable;

    protected $connection = 'zz_med_mysql';

    protected $table = 'z_hosp_member';

    public static $PASSWORD_SALT = 'omron@paSsw0rd';

    const CREATED_AT = 'create_at';

    const UPDATED_AT = 'update_at';

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'login_pass',
    ];

    // Rest omitted for brevity

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }

    /**
     * rewrite this method
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->login_pass;
    }

    /**
     *  通过手机号修改密码
     * @param $username
     * @param $mobile
     * @param $password
     * @return bool
     */
    public static function resetPassword($username, $mobile, $password)
    {
        return (bool)self::query()
            ->where('cell', $mobile)
            ->update(['login_pass' => md5($password . self::$PASSWORD_SALT)]);
    }


    /**
     *  获取医生的个人信息
     * @param $id
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder|object|null
     */
    public static function retrieveDoctorInfoById($id)
    {
        return self::query()->from('z_doc_info as a')
            ->select('a.*')
            ->where('a.hosp_member_id', $id)
            ->first();
    }


    /**
     *  获取医院信息
     * @param $hospId
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public static function getHospInfo($hospId)
    {

        return self::query()->from('z_hosp')->where('id', $hospId)->first();

    }


    /**
     *  获取登陆医生信息
     * @param $docId
     * @return array
     */
    public static function getDocInfo($docId)
    {
        $data = self::query()->from('z_hosp_member as a')
                ->select('a.*', 'b.dept_id', 'c.id as hosp_id', 'c.name as hosp_name', 'e.name as dept_name')
                ->leftJoin('z_hosp_member_dept as b', 'a.id', '=', 'b.hosp_member_id')
                ->leftJoin('z_hosp as c', 'c.id', '=', 'a.hosp_id')
                ->leftJoin('z_dept as d', 'd.id', '=', 'b.dept_id')
                ->leftJoin('z_t_dept as e', 'e.id', '=', 'd.t_dept_id')
                ->where('a.id', $docId)->get()
                ->toArray()[0] ?? [];
        if (!$data) {
            throw new \Exception("当前医生信息不存在，请核对后重试");
        }

        return $data;
    }
    
    
    /*
     * 获取医生名称
     */
    
        public static function getDocName($docId)
    {
        $data = self::query()->from('z_hosp_member as a') 
                ->where('a.id', $docId)->first(); 
        return $data->name ?? "";
    }

    /**
     *  获取医生所在科室
     * @param $docId
     * @return int|mixed
     */
    public static function getDocDeptId($docId)
    {

        return self::query()->from('z_hosp_member_dept')
                ->where('hosp_member_id', $docId)
                ->first()->dept_id ?? 0;

    }

    /**
     * 获取医生的权限菜单
     * @param int $docId
     * @param int $hospId
     * @return array
     */
    public static function getMenu(int $docId, int $hospId)
    {
        $info = self::query()->from('z_pituitary_doctor')
            ->where('doc_id', '=', $docId)
            ->first(['permit_menu', 'permit_btn']);
        $menuId = empty($info) ? 0 : $info->toArray()['permit_menu'];
        if (empty($menuId)) {
            return [];
        }

        $menuId = explode(',', $menuId);
        $list = self::query()->from('z_pituitary_menu_main')
            ->selectRaw('id,name,pid,url,icon')
            ->whereIn('id', $menuId)
            ->orderBy('pid', 'asc')
            ->orderBy('disorder', 'asc')
            ->get()->toArray();

        return self::treeMenu($list);
    }

    /**
     * 处理菜单树
     * @param array $data
     * @return array
     */
    public static function treeMenu(array $data)
    {
        if (empty($data)) {
            return $data;
        }

        $res = [];
        foreach ($data as $item) {
            if (!$item['pid']) {
                $res[$item['id']] = $item;
                $res[$item['id']]['child'] = [];
            } else {
                $res[$item['pid']]['child'][] = $item;
            }
        }

        return array_values($res);
    }

}